﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class PointsUDRcordEdit : AdminPage
    {
        BLL.CNVP_PointsUDRcord bll = new BLL.CNVP_PointsUDRcord();
        Model.CNVP_PointsUDRcord model = new Model.CNVP_PointsUDRcord();
        public int UserID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 89)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["UserID"]) && Public.IsNumber(Request.QueryString["UserID"]))
            {
                UserID = Convert.ToInt32(Request.QueryString["UserID"]);
            }
            if (!IsPostBack)
            {
                PointsNumInfo();
                lbTitle.Text = "添加";
            }
        }

        private void PointsNumInfo()
        {
            Model.CNVP_Users model_Users = new BLL.CNVP_Users().GetSingle_CNVP_UsersById(UserID);
            if (model_Users != null)
            {
                lbUserName.Text = model_Users.UserName;
                lbNowPointsNum.Text = model_Users.PointsNum.ToString();//先提取积分
            }

        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                int nowPointsNum = Convert.ToInt32(lbNowPointsNum.Text);//先提取积分
                int NowNoPointsNum = nowPointsNum + int.Parse(txtPointsNum.Text.Trim());
                if (NowNoPointsNum < 0)
                {
                    BasePage.MessageBoxGoHistory("当前积分不足！");
                    return;
                }
                if (NowNoPointsNum < nowPointsNum)
                {
                    try
                    {
                        var UserLevel_list = new BLL.CNVP_UserLevel().GetAll_CNVP_UserLevel(" order by NeedPointsNum");//大到小判断排序,降低等级
                        for (int i = 0; i < UserLevel_list.Count; i++)
                        {
                            int j = i + 1;
                            if (UserLevel_list[i].NeedPointsNum <= NowNoPointsNum && NowNoPointsNum < UserLevel_list[j].NeedPointsNum)
                            {
                                //更新用户等级
                                new BLL.CNVP_Users().UpdateUserLevelID(UserID, UserLevel_list[i].ID);
                            }
                        }
                    }
                    catch { }
                }
                else
                {
                    var UserLevel_list = new BLL.CNVP_UserLevel().GetAll_CNVP_UserLevel(" order by NeedPointsNum desc ");//大到小判断排序,提高等级
                    foreach (Model.CNVP_UserLevel u in UserLevel_list)
                    {
                        if (NowNoPointsNum >= u.NeedPointsNum)
                        {
                            //更新用户等级
                            new BLL.CNVP_Users().UpdateUserLevelID(UserID, u.ID);
                            break;
                        }
                    }
                }
                new BLL.CNVP_Users().UpdatePointsNum(UserID, NowNoPointsNum);//更新用户积分
                //再添加到记录中
                int rows = new BLL.CNVP_PointsUDRcord().Add_CNVP_PointsUDRcord(
                new Model.CNVP_PointsUDRcord()
                {
                    OperateTime = DateTime.Now.AddSeconds(1),
                    OperateDetail = txtOperateDetail.Text.Trim(),
                    PointsNum = int.Parse(txtPointsNum.Text.Trim()),
                    UserID = UserID
                });
                if (rows == 1)
                {
                    PointsNumInfo();
                    BasePage.MessageBox("添加成功！", "PointsUDRcordList.aspx?UserID=" + UserID + "");
                }
                else
                { BasePage.MessageBoxGoHistory("添加失败！"); }
            }
            catch
            {
                BasePage.MessageBoxGoHistory("添加失败！");
            }
        }
    }
}